//
//  SingerDealController.m
//  欢乐美食
//
//  Created by 遇水 on 15/6/24.
//  Copyright (c) 2015年 遇水. All rights reserved.
//

#import "SingerDealController.h"
#import "MSDealTool.h"
#import "buyIntension.h"
#import "SellerInfoView.h"
#import "SellerInfoHeaderView.h"

#import "UMSocialSnsService.h"
#import "UMSocialSnsPlatformManager.h"

#import "ImageAndTextDealController.h"

#import "Config.h"
#import "AboutDeals.h"

@interface SingerDealController () <AboutDealsDelegate>

@property (nonatomic, strong) UIScrollView    *scrollView;     //是整个view的根view
@property (nonatomic, strong) MSDeals    *result;   //获得的单个团购详情

@property (nonatomic, weak) SellerInfoHeaderView    *headerView;
@property (nonatomic, weak) SellerInfoView    *businessInfo;

@property (nonatomic, weak) buyIntension    *buyIntension;
@property (nonatomic, weak) AboutDeals    *aboutDeal;


//此页面的样式是使用scrollView来显示的，利用此控件加载xib中的内容
@end

@implementation SingerDealController


- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"团购详情";
    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    scrollview.alwaysBounceHorizontal = NO;
    [self.view addSubview:scrollview];
    self.scrollView = scrollview;
    
    //初始化内部的控件
    [self initNibView];
    //添加控件
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(share)];
    
    [self loadSingerDeal];
    
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushImageAndText)
                                                name:ImageAndTextNotification
                                              object:nil];
    [self loadMoreDeals];

}

-(void)pushImageAndText
{
    
    
    ImageAndTextDealController *imageAndText = [[ImageAndTextDealController alloc]init];
    imageAndText.deal = self.deal;
    
    [self.navigationController pushViewController:imageAndText animated:YES];
    
    
}
#pragma mark - 网络请求
-(void)loadSingerDeal
{
    [MBProgressHUD showMessage:@"加载..."];
    MSGetSingleDealParam *param = [[MSGetSingleDealParam alloc]init];
    param.deal_id = self.dealD;
    [MSDealTool getSingleDeal:param success:^(MSGetSingleDealResult *result) {
        //请求成功
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"团购详情加载成功"];
        
        self.result = result.deals[0];
        self.headerView.deal = self.result;
        self.headerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.headerView.frame.size.height);
        
        self.businessInfo.deal = self.result;
        self.businessInfo.frame = CGRectMake(0, self.headerView.y + self.headerView.height, self.view.bounds.size.width, self.businessInfo.height);
        
        self.buyIntension.deal = self.result;
        self.aboutDeal.frame = CGRectMake(0, self.businessInfo.height + self.businessInfo.y, self.view.bounds.size.width, self.aboutDeal.height);
        
        self.scrollView.contentSize = CGSizeMake(self.view.width, self.aboutDeal.y +self. aboutDeal.height);
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"团购详情加载失败"];
    }];
 }
//初始化nib文件
-(void)initNibView
{
    //头部信息
    SellerInfoHeaderView *headerView = [[[NSBundle mainBundle]loadNibNamed:@"SellerInfoHeaderView" owner:nil options:nil]lastObject];
    //（0，0， 320 ，286 ）
  
    self.headerView = headerView;
    //商户信息
    SellerInfoView *businessInfo = [[[NSBundle mainBundle]loadNibNamed:@"SellerInfoView" owner:nil options:nil ]lastObject];
    
    self.businessInfo = businessInfo;
    
    //关于信息
    AboutDeals *aboutDeal = [[[NSBundle mainBundle]loadNibNamed:@"AboutDeals" owner:nil options:nil]lastObject];
    
    self.aboutDeal = aboutDeal;
    self.aboutDeal.delegate = self;
    
    //添加到滚动视图中去
    [self.scrollView addSubview:headerView];
    [self.scrollView addSubview:businessInfo];
    [self.scrollView addSubview:aboutDeal];

}
//分享
-(void)share
{
    NSString *text = [NSString stringWithFormat:@"【%@】%@ 详情查看：%@", self.deal.title, self.deal.desc, self.deal.deal_url];
    
    // 需要分享的图片（不分享占位图片）
//    UIImage *image = nil;
//    if (self.iconView.image != [UIImage imageNamed:@"placeholder_deal"]) {
//        image = self.iconView.image;
//    }
    [UMSocialSnsService presentSnsController:self appKey:UMAppKey shareText:text shareImage:nil shareToSnsNames:@[UMShareToSina, UMShareToRenren] delegate:nil];
    
}

//更多
-(void)loadMoreDeals
{
    //发送一个网络请求
    MSFindDealsParam *param = [[MSFindDealsParam alloc]init];
    param.city = [[NSUserDefaults standardUserDefaults]stringForKey:@"city"];
    param.category = @"美食";
    param.limit = @(4);
    [MSDealTool findDeals:param success:^(MSFindDealsResult *result) {
       
    } failure:^(NSError *error) {
        
    }];
    
}


#pragma  mark SingerDealController代理方法
- (void)updateHomeViewControllerView:(SingerDealController *)controller dealId:(NSString *)dealId
{
    self.dealD = dealId;
    [self loadSingerDeal];
}



@end
